package com.acxiom.bsh.util;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {

	private static final String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
//	private static final SimpleDateFormat DATE_FORMAT_FULL = new SimpleDateFormat(FORMAT_FULL);
	
	private static final String FORMAT_DATE = "yyyy-MM-dd";
//	private static final SimpleDateFormat DATE_FORMAT_DATE = new SimpleDateFormat(FORMAT_DATE);
	
	private static final String FORMAT_FULL_NO_DELIMITER = "yyyyMMddHHmmss";
//	private static final SimpleDateFormat DATE_FORMAT_FULL_NO_DELIMITER = new SimpleDateFormat(FORMAT_FULL_NO_DELIMITER);
	
	private static final String FORMAT_DATE_NO_DELIMITER = "yyyyMMdd";
//	private static final SimpleDateFormat DATE_FORMAT_DATE_NO_DELIMITER = new SimpleDateFormat(FORMAT_DATE_NO_DELIMITER);

	private static final String FORMAT_MONTH_DATE_NO_DELIMITER = "yyyyMM";
//	private static final SimpleDateFormat DATE_FORMAT_MONTH_DATE_NO_DELIMITER = new SimpleDateFormat(FORMAT_MONTH_DATE_NO_DELIMITER);

	
	public static String format(Date date) {	
		return new SimpleDateFormat(FORMAT_FULL).format(date);
	}
	
	public static Date getDate(int days) {
		Date date = new Date(); 
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, days); 
		date = cal.getTime();		
		return date;
	}
	
	public static Date getDate(Date startDate, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		cal.add(Calendar.DATE, days); 
		Date date = cal.getTime();		
		return date;
	}
	
	public static Date getDateOffHours(int hours) {
		Date date = new Date(); 
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, hours); 
		date = cal.getTime();		
		return date;
	}
	
	public static Date stringToDate(String date){
		try {
			return new SimpleDateFormat(FORMAT_FULL).parse(date);
		} catch (Exception e) {
			return null;
		}
		
	}
	
	public static Date stringToSimpleDate(String date){
		try {
			return new SimpleDateFormat(FORMAT_DATE).parse(date);
		} catch (Exception e) {
			return null;
		}
		
	}
	
	public static String dateToSimpleString(Date date){
		if (date == null) {
			return null;
		}
		return new SimpleDateFormat(FORMAT_DATE).format(date);
	}
	
	public static String currentDate(){
		return new SimpleDateFormat(FORMAT_DATE).format(new Date());
	}
	
	public static String currentDateTime(){
		return new SimpleDateFormat(FORMAT_FULL).format(new Date());
	}
	
	public static String currentDateTimeWithNoDelimiter(){
		return new SimpleDateFormat(FORMAT_FULL_NO_DELIMITER).format(new Date());
	}
	
	public static String currentDateWithNoDelimiter(){
		return new SimpleDateFormat(FORMAT_DATE_NO_DELIMITER).format(new Date());
	}
	
	public static List<Date> getPreviousDays(int delay) {
		List<Date> days = new ArrayList<Date>();
		for (int i = delay; i >= 0; i--) {
			days.add(getPreviousDay(i));
		}
		return days;
	}
	
	private static Date getPreviousDay(int delay) {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		cal.add(Calendar.DATE, delay);
		return cal.getTime();
	}
	
	public static String getPreviousDayString(int delay) {
		Date date = getPreviousDay(delay);
		return new SimpleDateFormat(FORMAT_DATE).format(date);
	}
	
	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date); 
		return calendar.get(Calendar.YEAR);
	}
	
	public static Date getEndOfNextNaturlYear() {
		int year = getYear(new Date()) + 1;
		String date = year + "-12-31 23:59:59";
		return stringToDate(date);
	};
	
	public static int getNaturalWeek(Date date) {
		Calendar calendar = Calendar.getInstance();  
		calendar.setFirstDayOfWeek(Calendar.MONDAY);  
		calendar.setTime(date); 
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}	
	
	public static boolean isSameDay(Date date) {
		if (date == null) {
			return false;
		}
		return dateToSimpleString(date).equals(dateToSimpleString(new Date()));
	};
	
	public static boolean isSameNaturalWeek(Date date) {
		return isSameNaturalMonth(date) && getNaturalWeek(date) == getNaturalWeek(new Date());
	};
	
	public static boolean isSameNaturalMonth(Date date) {
		
		return new SimpleDateFormat(FORMAT_MONTH_DATE_NO_DELIMITER).format(date).equals(new SimpleDateFormat(FORMAT_MONTH_DATE_NO_DELIMITER).format(new Date()));
	};
	
	public static boolean isSameNaturalYear(Date date) {
		return getYear(date) == getYear(new Date());
	};
	
	public static boolean isSameDay(Date date, Date date2) {
		if (date == null) {
			return false;
		}
		return dateToSimpleString(date).equals(dateToSimpleString(date2));
	};
	
	public static boolean isSameNaturalWeek(Date date, Date date2) {
		return isSameNaturalMonth(date, date2) && getNaturalWeek(date) == getNaturalWeek(date2);
	};
	
	public static boolean isSameNaturalMonth(Date date, Date date2) {
		
		return new SimpleDateFormat(FORMAT_MONTH_DATE_NO_DELIMITER).format(date).equals(new SimpleDateFormat(FORMAT_MONTH_DATE_NO_DELIMITER).format(date2));
	};
	
	public static boolean isSameNaturalYear(Date date, Date date2) {
		return getYear(date) == getYear(date2);
	};
	
	public static Date buildDate(int year, int month, int date) {
		if (year > 0 && month > 0 && date >0) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month - 1, date);
			return calendar.getTime();
		} else {
			return null;
		}
	}
	
}
